﻿using HttpLib;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Nette
{
    class FileBox : StackPanel
    {

        public FileLine AddEmptyLine()
        {
            var line = new FileLine();
            line.IsSelected = false;
            line.IsCanDelete = false;
            //添加事件
            line.TLostFocus += onInput;
            line.DeleteClick += onDelClick;
            this.Children.Add(line);
            return line;
        }

        public FileBox PutLine(KVLine line)
        {
            //添加事件
            line.TLostFocus += onInput;
            line.DeleteClick += onDelClick;
            this.Children.Insert(0, line);
            return this;
        }


        private FileLine getLastLine()
        {
            return (FileLine)Children[Children.Count - 1];
        }
        private void deleteLine(FileLine kvLine)
        {
            //最后一个不能删除
            Children.Remove(kvLine);
        }


        public FileBox()
        {
            //初始化子元素事件
            foreach (FileLine line in Children)
            {
                line.TLostFocus += onInput;
                line.DeleteClick += onDelClick;
            }

            //默认有一个
            AddEmptyLine();
        }

        private void onInput(object sender, RoutedEventArgs e)
        {
            //查看如果输入内容为不为空字符且为最底部的节点，则添加节点
            FileLine taline = (FileLine)sender;
            if ((!taline.KeyStr.Equals("") || !taline.ValueStr.Equals("") || taline.Files.Length > 0) && taline.Equals(getLastLine()))
            {
                taline.IsCanDelete = true;
                taline.IsSelected = true;
                AddEmptyLine();
            }
        }

        private void onDelClick(object sender, RoutedEventArgs e)
        {
            deleteLine((FileLine)sender);
        }

        public List<Files> GetFiles()
        {
            List<Files> files = new List<Files>();
            foreach(FileLine line in Children) 
            {
                if (line.IsSelected == false) continue;
                var data = line.GetData();
                if (data.FType == FileLine.TYPE_FILE) 
                {
                   if(data.Files!= null && data.Files.Length>0 && !String.IsNullOrEmpty(data.Key)) 
                    {
                        foreach (string filename in data.Files)
                        {
                            files.Add(new Files(data.Key, filename));
                        }
                    }
                }
            }
            return files;
        }

        public List<Val> GetValues()
        {
            List<Val> vals = new List<Val>();
            foreach (FileLine line in Children)
            {
                if (line.IsSelected == false) continue;
                var data = line.GetData();
                if (data.FType == FileLine.TYPE_VALUE)
                {
                    if (!String.IsNullOrEmpty(data.Value) && !String.IsNullOrEmpty(data.Key))
                    {
                        vals.Add(new Val(data.Key, data.Value));
                    }
                }
            }
            return vals;
        }
    }
}
